package com.example.schoolnewsapp.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.schoolnewsapp.MainActivity;
import com.example.schoolnewsapp.R;
import com.example.schoolnewsapp.database.DBHelper;
import com.example.schoolnewsapp.util.SharedPreUtil;

public class LoginActivity extends AppCompatActivity {
    private ImageView imageView;
    private DBHelper dbHelper;
    private EditText username;
    private EditText password;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        imageView = (ImageView) findViewById(R.id.login_back);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, LoginOrRegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });

        dbHelper = new DBHelper(this, "UserDB.db", null, 6);
        button = (Button) findViewById(R.id.login_sub);
        username = (EditText) findViewById(R.id.login_username);
        password = (EditText) findViewById(R.id.login_password);



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getReadableDatabase();
                String username_str = username.getText().toString().trim();
                String password_str = password.getText().toString().trim();
                Cursor cursor = db.rawQuery("select * from User where name=?", new String[]{username_str});
                if (username_str !=null && password_str!=null) {
                    if (cursor.getCount() == 0) {
                        Toast.makeText(LoginActivity.this, "用户名不存在！", Toast.LENGTH_SHORT).show();
                    } else {
                        if (cursor.moveToFirst()) {
                            String password_db = cursor.getString(cursor.getColumnIndex("password"));
                            if (password_str.equals(password_db)) {
                                SharedPreUtil.setParam(LoginActivity.this, SharedPreUtil.IS_LOGIN, true);
                                SharedPreUtil.setParam(LoginActivity.this, SharedPreUtil.LOGIN_DATA, username_str);
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            } else {
                                Toast.makeText(LoginActivity.this, "密码错误，请重新登录", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(LoginActivity.this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
                        }
                        }
                    }
                    cursor.close();
                    db.close();
                }
            });

    }


}